package com.jjclub.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.jjclub.shiro.JcRelam;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Classname ShiroConfig
 * @Description shiro配置类
 * @Date 2019/10/25 12:54
 * @Created by jq
 */
@Configuration
public class ShiroConfig {


    //1、配置shiroFillter
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean ();
        //存放SecurityManger
        shiroFilter.setSecurityManager(securityManager);
        //返回登录正常页面
        shiroFilter.setSuccessUrl("/index");
        //返回登录失败页面
        shiroFilter.setLoginUrl("/login");
        //返回未认证页面
        shiroFilter.setUnauthorizedUrl("/login");

        Map<String,String> map = new LinkedHashMap();
        //存放过滤配置
        map.put("/login","anon");
        map.put("/loginAction","anon");
        map.put("/js/**", "anon"); //匿名访问静态资源
        map.put("/images/**", "anon"); //匿名访问静态资源
        map.put("/css/**", "anon"); //匿名访问静态资源
        map.put("/themes/**", "anon"); //匿名访问静态资源
        map.put("/fonts/**", "anon"); //匿名访问静态资源
        map.put("/json/**", "anon"); //匿名访问静态资源
        map.put("/layui/**", "anon"); //匿名访问静态资源
        map.put("/captcha/**","anon");
        //放行Swagger2页面，需要放行这些
        map.put("/swagger-ui.html","anon");
        map.put("/swagger/**","anon");
        map.put("/webjars/**", "anon");
        map.put("/swagger-resources/**","anon");
        map.put("/v2/**","anon");
        map.put("/**","authc");
        map.put("/logout","logout");
        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }
    //第二部创建SecurityManager
    @Bean
    public SecurityManager securityManager(JcRelam jcRelam){
        //使用DefaultWebSecurityManager 创建securityManager
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //设置加密次数
        hashedCredentialsMatcher.setHashIterations(2);
        jcRelam.setCredentialsMatcher(hashedCredentialsMatcher);
        DefaultWebSecurityManager  securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(jcRelam);
        return securityManager;
    }

    /**
     * description:在thymeleaf集成shiro
     * @param: []
     * @author: jj
     * @date: 2019/11/6 15:53
     * @return at.pollux.thymeleaf.shiro.dialect.ShiroDialect
     **/
     @Bean(name = "shiroDialect")
     public ShiroDialect shiroDialect() {
        return new ShiroDialect();
     }

        /**
         * description:生成密文方法
         * @param: [args]
         * @author: jj
         * @date: 2019/11/6 14:20
         * @return void
         **/
     public static void main(String[] args) {
         String hashAlgorithmName = "MD5";
         String credentials = "123456";
         int hashIterations = 2;
         ByteSource credentialsSalt = ByteSource.Util.bytes("ceshi");
         Object obj = new SimpleHash(hashAlgorithmName, credentials, credentialsSalt, hashIterations);
         System.out.println(obj);
     }
}
